class Solution {
    public int trap(int[] height) {
        int water = 0;
        int left = 0, right = height.length - 1;

        //这两个Max相当于两侧的墙
        int leftMax = 0,rightMax = 0;
        while (left < right) {
            leftMax = Math.max(leftMax,height[left]);
            rightMax = Math.max(rightMax,height[right]);
            if(height[left] < height[right]){
                water += leftMax - height[left];
                left ++;
            }else{
                water += rightMax - height[right];
                right --;
            }

        }
        return water;
    }
}